package com.kim.bookshop.order.serviceimpl;

import java.sql.SQLException;
import java.util.List;
import cn.itcast.jdbc.JdbcUtils;

import com.kim.bookshop.book.dao.BookDao;
import com.kim.bookshop.order.bean.Order;
import com.kim.bookshop.order.bean.OrderItem;
import com.kim.bookshop.order.dao.OrderDao;
import com.kim.bookshop.order.service.OrderService;
import exception.BookShopException;

public class OrderServiceImpl implements OrderService {

	private OrderDao orderDao;
	private BookDao bookDao;
	
	@Override
	public void addOrder(Order order) {
		try{
			//开启事务
			JdbcUtils.beginTransaction();
			orderDao.addOrder(order);
			orderDao.addOrderItemList(order.getOrderItemList());
			
			//提交事务
			JdbcUtils.commitTransaction();
			
			
		}catch (Exception e) {
			//回滚事务
			try {
				JdbcUtils.rollbackTransaction();
			} catch (SQLException e2) {
			}
			throw new RuntimeException(e);
		}
		
	}

	@Override
	public List<Order> myOrders(String uid) {
		return orderDao.findByUid(uid);
	}

	@Override
	public Order load(String oid) {
		return orderDao.load(oid);
	}

	@Override
	public void confirm(String oid) throws BookShopException {
		int state=orderDao.getStateByOid(oid);
		if(state!=3) throw new BookShopException("确认收货失败！！！");
		orderDao.updateState(oid, 4);
		
	}

	@Override
	public void pay(String oid) {
		/*
		 * 获取订单状态
		 * 若为1，执行修改订单状态,库存变更
		 * 否则，什么都不做
		 */
		System.out.println("订单编号:"+oid);
		int state =orderDao.getStateByOid(oid);
		if(state==1){//更改订单状态，库存变更
			orderDao.updateState(oid, 2);
			List<Order> listOrder=  orderDao.findByOid(oid);
			for (Order order : listOrder) {
				List<OrderItem>  orderItems= order.getOrderItemList();
				for (OrderItem orderItem : orderItems) {
					String bid=orderItem.getBook().getBid();
					System.out.println("书名"+orderItem.getBook().getBname()+"  编号："+bid);
					
					int preStock=bookDao.getStockByBid(bid);//获取该书的库存量
					System.out.println("库存："+preStock);
					
					int count=orderItem.getCount();//获取下单数量
					System.out.println("数量："+count);
					bookDao.updateStock(bid, (preStock-count));//根据下单的数目，库存相应变更
				}
			}
		}
		
	}

	@Override
	public List<Order> findAll() {
		return orderDao.findAll();
	}
	@Override
	public void deliver(String oid) throws BookShopException {
		int state=orderDao.getStateByOid(oid);
		if(state!=2) throw new BookShopException("发收货失败！！！");
		orderDao.updateState(oid, 3);
		
	}

	@Override
	public List<Order> unpayorder() {
		return orderDao.unpayorder();
	}

	@Override
	public List<Order> paidorder() {
		return orderDao.paidorder();
	}

	@Override
	public List<Order> unconfirm() {
		return orderDao.unconfirm();
	}

	@Override
	public List<Order> success() {
		return orderDao.success();
	}

	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}

	public void setBookDao(BookDao bookDao) {
		this.bookDao = bookDao;
	}


	
	
	
}
